package cn.shentianlan.service.impl;

import cn.shentianlan.criteria.TransactionProcessingCriteria;
import cn.shentianlan.domain.dto.TransactionProcessingDTO;
import cn.shentianlan.domain.entity.TransactionProcessing;
import cn.shentianlan.mapper.TransactionProcessingMapper;
import cn.shentianlan.service.TransactionProcessingService;
import cn.shentianlan.service.UserService;
import cn.shentianlan.utils.BaseUtils;
import cn.shentianlan.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 陈俊源
 * @Description TODO
 * @Date 2021/5/7
 * @Version 1.0
 */
@Service
public class
TransactionProcessingServiceImpl implements TransactionProcessingService {
    @Autowired
    private TransactionProcessingMapper transactionProcessingMapper;
    @Autowired
    private UserService userService;

    @Override
    public Result saveOrUpdate(TransactionProcessingDTO dto) {
        if (dto==null){
            return Result.fail("参数对象不能为空");
        }
        if (dto.getId()==null){
            if (dto.getApplicationTime() == null){
                dto.setApplicationTime(LocalDateTime.now());
                dto.setAuditStatus("审核中");
            }
            if (dto.getAuditorId()!=null && dto.getApplicationTime() ==null){
                dto.setAuditTime(LocalDateTime.now());
                dto.setAuditStatus("已审核");
            }
            transactionProcessingMapper.save(dto);
        }else{
            if (dto.getAuditTime() == null || dto.getAuditorId()!=null ){
                dto.setAuditTime(LocalDateTime.now());
            }
            transactionProcessingMapper.update(dto);
        }
        return Result.success();
    }

    @Override
    public Result findTransactionProcessingByCriteria(TransactionProcessingCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize(), false);
        Integer count = transactionProcessingMapper.getCount(criteria);
        List<TransactionProcessing> transactionProcessingList = transactionProcessingMapper.findTransactionProcessingByCriteria(criteria);
        List<TransactionProcessingDTO> transactionProcessingDTOList = new ArrayList<TransactionProcessingDTO>();
        for (TransactionProcessing transactionProcessing :transactionProcessingList ) {
            TransactionProcessingDTO transactionProcessingDTO = bulidTransactionProcessingDTO(transactionProcessing);
            transactionProcessingDTOList.add(transactionProcessingDTO);
        }
        PageInfo<TransactionProcessingDTO> pageInfo = new PageInfo<TransactionProcessingDTO>(transactionProcessingDTOList);
        pageInfo.setTotal(count.longValue());
        return Result.success(pageInfo);
    }

    @Override
    public Result delete(Long id) {
        if (id==null){
            return Result.fail("要删除的数据id不能为空");
        }
        TransactionProcessing transactionProcessingById = transactionProcessingMapper.getTransactionProcessingById(id);
        if (transactionProcessingById == null){
            return Result.fail("无法获取到相应数据信息,删除失败");
        }
        transactionProcessingMapper.deleteTransactionProcessingById(id);
        return Result.success();
    }

    @Override
    public Result getTransactionProcessing(Long id) {
        if (id==null){
            return Result.fail("id不能为空");
        }
        TransactionProcessing transactionProcessing = transactionProcessingMapper.getTransactionProcessingById(id);
        if (transactionProcessing == null){
            return Result.fail("无法获取到相应数据信息");
        }
        TransactionProcessingDTO transactionProcessingDTO = bulidTransactionProcessingDTO(transactionProcessing);
        return Result.success(transactionProcessingDTO);
    }

    private TransactionProcessingDTO bulidTransactionProcessingDTO(TransactionProcessing transactionProcessing){
        TransactionProcessingDTO transactionProcessingDTO = BaseUtils.copyProperties(transactionProcessing, TransactionProcessingDTO.class);
        if (transactionProcessing.getUserId()!=null){
            String userName = userService.getFullName(transactionProcessing.getUserId());
            transactionProcessingDTO.setUserName(userName);
        }
        if (transactionProcessing.getAuditorId()!=null){
            String auditorName = userService.getFullName(transactionProcessing.getAuditorId());
            transactionProcessingDTO.setAuditorName(auditorName);
        }
        return transactionProcessingDTO;
    }
}
